<?php

class AuthassignmentController extends AdminController
{

    public function actionIndex()
    {
        //Init values
        $model = AuthItem::model();
        $data['userAssignedRoles'] = array();
        $data['userNotAssignedRoles'] = array();
        $data['roleAssignedTasks'] = array();
        $data['roleNotAssignedTasks'] = array();
        $data['taskAssignedOpers'] = array();
        $data['taskNotAssignedOpers'] = array();
        $data['taskNotAssignedOpers'] = array();
        $data['assignedClients'] = array();
        $data['unassignedClients'] = array();
        
        //$data['usersByName'] = User::model()->findAllUsersByName();
        $name = isset($_POST["txtFind"]) ? $_POST["txtFind"] : "";
        $data['usersByName'] = User::model()->findAllUsersByName($name);


        $data["assign"] = array("disabled" => true, "class" => "button arrow-left disabled");
        $data["revoke"] = array("disabled" => true, "class" => "button arrow-right disabled");

        if (!Yii::app()->request->isAjaxRequest)
        {
            $this->render('index', array(
                'model' => $model,
                'data' => $data,
                    )
            );
        }
    }

    /**
     * Assigns roles to a user
     *
     * @param int $userid The user's id
     * @param String $roles The roles to assign
     * @param String $bizRules Not used yet
     * @param String $data Not used yet
     */
    private function _assignUser($userid, $roles, $bizRules, $data)
    {
        
         if ($userid)
        {
           $auth = Yii::app()->authManager;
            /* @var $auth CDbAuthManager */
            foreach ($roles as $role)
            {
                $auth->assign($role, $userid, $bizRules, $data);
            }
        }

    }

    /**
     * Revokes roles from a user
     * @param int $userid The user's id
     * @param String $roles The roles to revoke
     */
    private function _revokeUser($userid, $roles)
    {
        if ($userid)
        {
            $auth = Yii::app()->authManager;
            /* @var $auth CDbAuthManager */
            foreach ($roles as $role)
            {
               $auth->revoke($role, $userid);
            }
            return true;
        }

    }

    /**
     * Assigns child items to a parent item
     * @param String $parent The parent item
     * @param String $children The child items
     */
    private function _assignChild($parent, $children)
    {
        if ($parent)
        {
            $auth = Yii::app()->authManager;
            /* @var $auth CDbAuthManager */
            foreach ($children as $child)
            {
                $auth->addItemChild($parent, $child);
            }
        }
    }

    /**
     * Revokes child items from a parent item
     * @param String $parent The parent item
     * @param String $children The child items
     */
    private function _revokeChild($parent, $children)
    {
        if ($parent)
        {
            $auth = Yii::app()->authManager;
            /* @var $auth CDbAuthManager */
            foreach ($children as $child)
            {
               $auth->removeItemChild($parent, $child);
            }
        }
    }
    
     /**
     * Used by Ajax to get the roles of a user when he is selected in the Assign
     * roles to user tab
     */
    public function actionGetClients()
    {
        $this->_getTheClients();
    }

  
    /**
     * Used by Ajax to get the roles of a user when he is selected in the Assign
     * roles to user tab
     */
    public function actionGetRoles()
    {
        $this->_getTheRoles();
    }

    /**
     * Gets the assigned and not assigned roles of the selected user
     */
    private function _getTheRoles()
    {
        $model = new AuthItem();
        $userid = $_POST["users"];
        //$userid =  mysql_real_escape_string($userid);
        
        $data['userAssignedRoles'] = RDbAuthManager::getUserAssignedRoles($userid);
        $data['userNotAssignedRoles'] = RDbAuthManager::getUserNotAssignedRoles($userid);
        if ($data['userAssignedRoles'] == array())
        {
            $data['revoke'] = array("name" => "revokeUser", "disabled" => true, "class" => "button arrow-right disabled");
        }
        else
        {
            $data['revoke'] = array("name" => "revokeUser", "class" => "button arrow-right");
        }
        if ($data['userNotAssignedRoles'] == array())
        {
            $data['assign'] = array("name" => "assignUser", "disabled" => true, "class" => "button arrow-left disabled");
        }
        else
        {
            $data['assign'] = array("name" => "assignUser", "class" => "button arrow-left");
        }
        $this->renderPartial('_userAjax', array('model' => $model, 'data' => $data), false, true);
    }

    /**
     * Used by Ajax to get the tasks of a role when it is selected in the Assign
     * tasks to roles tab
     */
    public function actionGetTasks()
    {
        $this->_getTheTasks();
    }
    
    /**
     * Gets the assigned and not assigned tasks of the selected user
     */
    private function _getTheTasks()
    {
        $model = new AuthItem();
        $name = isset($_POST["roles"]) ? $_POST["roles"] : "";
        
        //$name =  mysql_real_escape_string($name);
        
        
        $data['roleAssignedTasks'] = RDbAuthManager::getRoleAssignedTasks($name);
        $data['roleNotAssignedTasks'] = RDbAuthManager::getRoleNotAssignedTasks($name);
        if ($data['roleAssignedTasks'] == array())
        {
            $data['revoke'] = array("name" => "revokeTask", "disabled" => true, "class" => "button arrow-right disabled");
        }
        else
        {
            $data['revoke'] = array("name" => "revokeTask", "class" => "button arrow-right");
        }
        if ($data['roleNotAssignedTasks'] == array())
        {
            $data['assign'] = array("name" => "assignTask", "disabled" => true, "class" => "button arrow-left disabled");
        }
        else
        {
            $data['assign'] = array("name" => "assignTask", "class" => "button arrow-left");
        }
        $this->renderPartial('_roleAjax', array('model' => $model, 'data' => $data), false, true);
    }

    /**
     * Used by Ajax to get the operations of a task when he is selected in the Assign
     * operations to tasks tab
     */
    public function actionGetOpers()
    {
        $this->_getTheOpers();
    }

    /**
     * Gets the assigned and not assigned operations of the selected user
     */
    private function _getTheOpers()
    {
        $model = new AuthItem();
        $name = isset($_POST["tasks"]) ? $_POST["tasks"] : "";
        //$name =  mysql_real_escape_string($name);
        
        $data['taskAssignedOpers'] = RDbAuthManager::getTaskAssignedOpers($name, false);
        $data['taskNotAssignedOpers'] = RDbAuthManager::getTaskNotAssignedOpers($name, false);

        if ($data['taskAssignedOpers'] == array())
        {
            $data['revoke'] = array("name" => "revokeOpers", "disabled" => true, "class" => "button arrow-right disabled");
        }
        else
        {
            $data['revoke'] = array("name" => "revokeOpers", "class" => "button arrow-right");
        }
        if ($data['taskNotAssignedOpers'] == array())
        {
            $data['assign'] = array("name" => "assignOpers", "disabled" => true, "class" => "button arrow-left disabled");
        }
        else
        {
            $data['assign'] = array("name" => "assignOpers", "class" => "button arrow-left");
        }
        $this->renderPartial('_taskAjax', array('model' => $model, 'data' => $data), false, true);
    }

    public function actionAssign()
    {
        $userid = isset($_POST["users"]) ? $_POST["users"] : "";
        //$userid =  mysql_real_escape_string($userid);
        
        $authItemName = isset($_POST['roles']) ? $_POST['roles'] : "";
        //$authItemName =  mysql_real_escape_string($authItemName);
        
        $assItemName = isset($_POST['tasks']) ? $_POST['tasks'] : "";
        //$assItemName =  mysql_real_escape_string($assItemName);

        $authItemAssignName = isset($_POST['assign']) ? $_POST['assign'] : "";
       
        
        $authItemRevokeName = isset($_POST['revoke']) ? $_POST['revoke'] : "";
        
        
        $assignRoles = Yii::app()->request->getParam('assignRoles', 0);
        $revokeRoles = Yii::app()->request->getParam('revokeRoles', 0);
        
        $assignTasks = isset($_GET['assignTasks']) ? $_GET['assignTasks'] : 0;
        
        
        $revokeTasks = isset($_GET['revokeTasks']) ? $_GET['revokeTasks'] : 0;
        
        
        $assignOpers = isset($_GET['assignOpers']) ? $_GET['assignOpers'] : 0;
        
        
        $revokeOpers = isset($_GET['revokeOpers']) ? $_GET['revokeOpers'] : 0;
        

        if ($assignRoles && is_array($authItemAssignName))
        {
            $this->_assignUser($userid, $authItemAssignName, "", "");
        }

        if ($revokeRoles && is_array($authItemRevokeName))
        {
            $this->_revokeUser($userid, $authItemRevokeName);
        }

        if ($assignTasks && is_array($authItemAssignName))
        {
            $this->_assignChild($authItemName, $authItemAssignName);
        }

        if ($revokeTasks && is_array($authItemRevokeName))
        {
            $this->_revokeChild($authItemName, $authItemRevokeName);
        }

        if ($assignOpers && is_array($authItemAssignName))
        {
            $this->_assignChild($assItemName, $authItemAssignName);
        }

        if ($revokeOpers && is_array($authItemRevokeName))
        {
            $this->_revokeChild($assItemName, $authItemRevokeName);
        }


        // assign to user show the user tab
        if ($userid != "")
        {
            $this->_getTheRoles();
        }
        else if ($assignTasks != 0 || $revokeTasks != 0)
        {
            $this->_getTheTasks();
        }
        else if ($assignOpers != 0 || $revokeOpers != 0)
        {
            $this->_getTheOpers();
        }
    }
   
}